package edu.hit.pact.pgse.client;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

import edu.hit.pact.pgse.bean.TaskPiece;
import edu.hit.pact.pgse.client.database.Source;
import edu.hit.pact.pgse.client.database.SourceDao;
import edu.hit.pact.pgse.crawler.bbs.BBSTaskPiece;
import edu.hit.pact.pgse.crawler.bbs.BBSUnit;
import edu.hit.pact.pgse.crawler.web.WebTaskPiece;
import edu.hit.pact.pgse.util.Utilities;

public class TaskPiecesConstructor {

	/**
	 * read task from database
	 * @return
	 */
	public static ArrayList<TaskPiece> webPieceGen() {
		ArrayList<TaskPiece> taskPieces = new ArrayList<TaskPiece>();
		WebTaskPiece taskPiece = null;
		SourceDao sdao = new SourceDao();
		
		try {
			List<Source> slist = sdao.querySource();
			Iterator<Source> siter = slist.iterator();
			while (siter.hasNext()) {
				Source source = siter.next();
				long id = (long)(Math.random()*1000*1000*1000);
				
				//get all 
				String homePage = source.getHomepage();
				String domain = source.getDomain();
				int depth = source.getDepth();
				int pageNumber = source.getPagenumber();
				int category = source.getCategory();
				String blongTo = source.getBelongto();
				String channel = source.getChannel();
				String place = source.getPlace();
				int priority = source.getPriority();
				
				//set all
				taskPiece = new WebTaskPiece(id);
				taskPiece.setHomePage(homePage);
				taskPiece.setMaxDepth(depth);
				taskPiece.setMaxPageNum(pageNumber);
				taskPiece.setType(category);
				taskPiece.setBelongTo(blongTo);
				taskPiece.setChannel(channel);
				taskPiece.setPlace(place);
				taskPiece.setPriority(priority);
				
				try {
					Thread.sleep(10);//防止开始时间相同，造成WebManager中写入waitting队列中时出错
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				taskPiece.setStartTime(Utilities.getDateInMillisecond());
				taskPieces.add(taskPiece);
				
				System.out.println(taskPiece.toString());
			}
			return taskPieces;

		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
	
	/**
	 * read task from file
	 * @param file
	 * @return
	 */
	public static ArrayList<TaskPiece> webPieceGen(String file) {
		ArrayList<TaskPiece> taskPieces = new ArrayList<TaskPiece>();
		WebTaskPiece taskPiece = null;
		Scanner scanner;
		try {
			scanner = new Scanner(new FileReader(file));
			while (scanner.hasNext()) {
				long id = (long)(Math.random()*1000*1000*1000);
				String homePage = scanner.next();
				String docURLRegexInclude = scanner.next();
//				String docURLRegexExclude = scanner.next();
//				String dateString = scanner.next();
//				int depth = scanner.nextInt();
				int pageNumber = scanner.nextInt();
				int depth = 10;
//				int pageNumber = 500;
				taskPiece = new WebTaskPiece(id);
				taskPiece.setHomePage(homePage);
				taskPiece.setDocURLRegexInclude(docURLRegexInclude);
//				taskPiece.setDocURLRegexExclude(docURLRegexExclude);
//				taskPiece.setDateString(dateString);
				
				taskPiece.setType(1);
				taskPiece.setMaxDepth(depth);
				taskPiece.setMaxPageNum(pageNumber);
				try {
					Thread.sleep(10);//防止开始时间相同，造成WebManager中写入waitting队列中时出错
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				taskPiece.setStartTime(Utilities.getDateInMillisecond());
				taskPieces.add(taskPiece);
			}
			return taskPieces;

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

	
	public static ArrayList<TaskPiece> bbsPieceGen(String file){
		int batch = 1;
		ArrayList<TaskPiece> taskPieces = new ArrayList<TaskPiece>();
		
		try {
			Scanner scanner = new Scanner(new FileReader(file));
			
			while (scanner.hasNext()){
				long id = (long)(Math.random()*1000*1000*1000);
				String url = null;
				int maxPages = 0;
				ArrayList<String> articlePrefixs = null;
				
				for (int i=0; i<batch && scanner.hasNext(); i++) {
					url = scanner.next();
					String articlePrefix = scanner.next();
					maxPages = scanner.nextInt();
					articlePrefixs = new ArrayList<String>();
					articlePrefixs.add(articlePrefix);
				}
				
				BBSTaskPiece taskPiece = new BBSTaskPiece(id, new BBSUnit(new URL(url), articlePrefixs, maxPages));
				taskPiece.setHomePage(url);
				taskPiece.setType(3);// 3 represents forum
				taskPiece.setStartTime(Utilities.getDateInMillisecond());
				taskPiece.setMaxPageNum(batch*100);
				taskPieces.add(taskPiece);
			}
			
			return taskPieces;

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static ArrayList<TaskPiece> blogPieceGen(String file){
		int batch = 1;
		ArrayList<TaskPiece> taskPieces = new ArrayList<TaskPiece>();
		
		try {
			Scanner scanner = new Scanner(new FileReader(file));
			
			while (scanner.hasNext()){
				long id = (long)(Math.random()*1000*1000*1000);
				String url = null;
				for (int i=0; i<batch && scanner.hasNext(); i++) {
					url = scanner.next();
				}
				
				TaskPiece taskPiece = new TaskPiece(id);
				taskPiece.setHomePage(url);
				taskPiece.setType(2);//2 represents blog
				taskPiece.setStartTime(0L);
				taskPiece.setMaxPageNum(batch);
				taskPieces.add(taskPiece);
			}
			
			return taskPieces;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}
	
	public static void main(String[] args){
		TaskPiecesConstructor.blogPieceGen("conf/task_blog");
	}
}
